<template>
  <a-modal
    :title="$t('button.resetPw')"
    :width="480"
    :maskClosable="false"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="close"
  >
    <a-form-model ref="form" :model="form" :label-col="{ span: 4 }" :wrapper-col="{ span: 18 }">
      <a-form-model-item
        :rules="[
          {
            required: true,
            message: $t('common.emptyErr', { name: $t('common.password').toLowerCase() }),
            trigger: 'change'
          },
          { validator: validateToNextPassword, trigger: 'change' }
        ]"
        prop="password"
      >
        <span slot="label">
          {{ $t('common.password') }}
          <a-tooltip :title="passwordTips">
            <a-icon type="question-circle-o" />
          </a-tooltip>
        </span>
        <a-input-password v-if="visible" v-model.trim="form.password">
          <a slot="addonAfter" @click="handleRandom">
            {{ $t('authenticationCenter.org.randomGeneration') }}
          </a>
        </a-input-password>
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>
<script>
import { randomString } from '@/utils/util'
import { resetPwd } from '@/api/authenticationCenter/structure'
import md5 from 'md5'
export default {
  data () {
    return {
      passwordInfo: {
        passwordMin: 6,
        passwordMax: 12,
        passwordRule: 'SIMPLE'
      },
      form: {
        id: null,
        password: '111111'
      },
      visible: false,
      confirmLoading: false,
      passwordRule: {
        SIMPLE: this.$t('authenticationCenter.securityConfiguration.numbersAndeLetters'),
        COMPLEX: this.$t('authenticationCenter.securityConfiguration.numbersOrLettersOrcharacter')
      }
    }
  },
  computed: {
    passwordTips () {
      const str = `${this.$t('modules.user.passwordLength')}${this.passwordInfo.passwordMin}-${
        this.passwordInfo.passwordMax
      },${this.passwordRule[this.passwordInfo.passwordRule]}`
      return str + ';' + this.$t('authenticationCenter.securityConfiguration.initPsw')
    }
  },
  methods: {
    open (id, passwordInfo) {
      this.form.id = id
      this.passwordInfo = passwordInfo
      this.visible = true
    },
    close () {
      this.form = {
        id: null,
        password: '111111'
      }
      this.passwordInfo = {
        passwordMin: 6,
        passwordMax: 12,
        passwordRule: 'SIMPLE'
      }
      this.visible = false
    },
    handleSubmit () {
      this.confirmLoading = true
      this.$refs.form.validate(valid => {
        if (valid) {
          resetPwd({
            id: this.form.id,
            password: md5(this.form.password)
          })
            .then(res => {
              this.$message.success(res.message)
              this.close()
            })
            .finally(() => {
              this.confirmLoading = false
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    // 密码校验
    validateToNextPassword (rules, value, callback) {
      if (value && value !== '111111') {
        const str = `${this.$t('modules.user.passwordLength')}${this.passwordInfo.passwordMin}-${
          this.passwordInfo.passwordMax
        },${this.passwordRule[this.passwordInfo.passwordRule]}`
        if (this.passwordInfo.passwordRule === 'COMPLEX') {
          if (value.length >= this.passwordInfo.passwordMin && value.length <= this.passwordInfo.passwordMax) {
            if (
              /^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\W_!@#$%^&*`~()-+=]+$)(?![a-z0-9]+$)(?![a-z\W_!@#$%^&*`~()-+=]+$)(?![0-9\W_!@#$%^&*`~()-+=]+$)[a-zA-Z0-9\W_!@#$%^&*`~()-+=]{6,}$/.test(
                value
              )
            ) {
              callback()
            } else {
              callback(str)
            }
          } else {
            callback(str)
          }
        } else if (this.passwordInfo.passwordRule === 'SIMPLE') {
          if (
            value.length >= this.passwordInfo.passwordMin &&
            value.length <= this.passwordInfo.passwordMax &&
            /[\d]/.test(value) &&
            /[a-zA-z]/.test(value)
          ) {
            callback()
          } else {
            callback(str)
          }
        }
      } else {
        callback()
      }
    },
    // 随机生成密码
    handleRandom () {
      this.form.password = randomString(this.passwordInfo.passwordMin, this.passwordInfo.passwordRule)
      this.$refs.form.validateField('password')
    }
  }
}
</script>
